#include "LoadState.h"
#include "LoadGameObjects.h"
#include "Game.h"
#include "TestState.h"
#include "DX11ShaderManager.h"
#include "DX11TextureManager.h"
#include "ObjMeshManager.h"
#include "DX11HeightMapTerrain.h"
#include "UIChat.h"
#include "ConfigManager.h"
#include "Text.h"
#include "DX11HelpfulFunctions.h"
#include "ScreenManager.h"
#include "TitleScreenState.h"

LoadState::LoadState()
{
	m_clearCol = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
	m_screenSize.x = (float)TheScreenManager::Instance()->GetWindowWidth();
	m_screenSize.y = (float)TheScreenManager::Instance()->GetWindowHeight();

}

void LoadState::UpdateBackgroundTexture(const std::wstring& texturepath)
{
	DX11::ClearColour(m_clearCol.x, m_clearCol.y, m_clearCol.z, m_clearCol.w);

	D3DXMATRIX m_view;
	D3DXMATRIX m_proj;

	D3DXMatrixLookAtLH(&m_view, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), 
		&D3DXVECTOR3(0.0f, 0.0f, 0.0f), 
		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
	
	D3DXMatrixOrthoLH(&m_proj, m_screenSize.x, m_screenSize.y, 0, 100);

	TheScreenManager::Instance()->SetViewProjMatrix(m_view, m_proj);
		
	TheScreenManager::Instance()->EnableBlending();
	TheObjMeshManager::Instance()->ChangeTexture("title_quad",
		TheDX11TextureManager::Instance()->UseTexture(texturepath));
	//TheObjMeshManager::Instance()->Translate("title_quad", D3DXVECTOR3(0.0f, -1.0f, rot));
	TheObjMeshManager::Instance()->Scale("title_quad", D3DXVECTOR3(m_screenSize.x, m_screenSize.y, 1.0f));
	//TheObjMeshManager::Instance()->Rotate("title_quad", M_PI_4, 1.0f, 0.0f, 0.0f);
	TheObjMeshManager::Instance()->Draw("title_quad");
	TheScreenManager::Instance()->DisableBlending();
}

void LoadState::Draw()
{

}

void LoadState::Update()
{

}

void LoadState::LoadMeshes()
{
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/minecraft_text.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/eras_text.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/default_texture.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/loading_images_meshes.png");
	TheDX11ShaderManager::Instance()->LoadShader("FontShader", L"FontShader.hlsl", "PS", "VS");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/crate_test.obj", "title_quad", L"TitleScreenQuad.hlsl");
	
	
	// loading meshes
	UpdateBackgroundTexture(L"../../Assets/loading_images_meshes.png");

	TheScreenManager::Instance()->FlipBuffers();
	
	TheFontManager::Instance()->LoadFont("mc");
	TheFontManager::Instance()->LoadFont("eras");
	

	//TheObjMeshManager::Instance()->LoadObj("../../Assets/crate_test.obj", "crate", L"Tutorial02.fx");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/sphere_test2.obj", "sphere", L"Tutorial02.hlsl");
	////TheObjMeshManager::Instance()->LoadObj("../../Assets/torus_test.obj", "torus", L"Tutorial02.fx");
	//TheObjMeshManager::Instance()->LoadObj("../../Assets/torus_test2.obj", "torus2", L"Tutorial02.fx");
	//TheObjMeshManager::Instance()->LoadObj("../../Assets/pyramid_test.obj", "pyramid", L"Tutorial02.fx");
	////TheObjMeshManager::Instance()->LoadObj("../../Assets/honda_jazz_1.obj", "honda_jazz", L"Tutorial02.fx");
	////TheObjMeshManager::Instance()->LoadObj("../../Assets/textured_rock_vsmall.obj", "rock", L"Tutorial02.fx");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/crate_textured.obj", "crate_textured", L"Tutorial02.hlsl");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/maze_floor.obj", "maze_floor", L"MazeFloorShader.hlsl");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/ground.obj", "floor", L"FloorShader.hlsl");
	//TheObjMeshManager::Instance()->LoadObj("../../Assets/post_process_quad.obj", "post_process", L"PostProcess.hlsl");
	////TheDX11ShaderManager::Instance()->LoadShader("post_process", "PostProcess.hlsl", "PS", "VS");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/agent.obj", "agent", L"Tutorial02.hlsl");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/agent_cube.obj", "agent_cube", L"Tutorial02.hlsl");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/player_cube.obj", "player_cube", L"Tutorial02.hlsl");
	TheObjMeshManager::Instance()->LoadObj("../../Assets/mini_map_quad.obj", "mini_map", L"MiniMapShader.hlsl");
		
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/player_arrow.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/floor_texture.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/wall_texture_small.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/fog_of_war.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/enemy_texture.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/title_image.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/options_image.png");
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/tutorial_image.png");


	// maze loads
	//TheObjMeshManager::Instance()->LoadObj("../../Assets/southWall.obj", "SouthWall", L"WallShader.hlsl");
	//TheObjMeshManager::Instance()->LoadObj("../../Assets/westWall.obj", "WestWall", L"WallShader.hlsl");
	//TheObjMeshManager::Instance()->LoadObj("../../Assets/southDoor.obj", "SouthDoor", L"WallShader.hlsl");
	//TheObjMeshManager::Instance()->LoadObj("../../Assets/westDoor.obj", "WestDoor", L"WallShader.hlsl");
		
	TheUIChatState::Instance();
}

void LoadState::LoadShaders()
{	
	DX11::ClearColour(m_clearCol.x, m_clearCol.y, m_clearCol.z, m_clearCol.w);
	// load shaders
	
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/loading_images_shaders.png");
	UpdateBackgroundTexture(L"../../Assets/loading_images_shaders.png");

	TheScreenManager::Instance()->FlipBuffers();


	//TheDX11ShaderManager::Instance()->LoadShader("TerrainShader", L"TerrainShader.hlsl", "PS", "VS");
	//TheDX11ShaderManager::Instance()->LoadShader("WaterShader", L"WaterShader.hlsl", "PS", "VS");
	//TheDX11ShaderManager::Instance()->LoadShader("LightViewRender", L"LightViewRender.hlsl", "PS", "VS");
	//TheDX11ShaderManager::Instance()->LoadShader("ShadowPostProcessor", L"ShadowPostProcessor.hlsl", "PS", "VS");
	//TheDX11ShaderManager::Instance()->LoadShader("Reflection", L"Reflection.hlsl", "PS", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("CrateShader", L"CrateShader.hlsl", "PS", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("DefaultTexture", L"DefaultTextureShader.hlsl", "PS", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("lightShader", L"Tutorial02.hlsl", "PSSolid", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("RoomShader", L"Tutorial02.hlsl", "PS", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("DefaultColour", L"Tutorial02.hlsl", "PS", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("DepthBuffer", L"DepthBuffer.hlsl", "PS", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("MiniMapBlurShader", L"MiniMapBlurShader.hlsl", "PS", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("MiniMapShader2", L"MiniMapShader2.hlsl", "PS", "VS");
	TheDX11ShaderManager::Instance()->LoadShader("MiniMapClearShader", L"MiniMapClearShader.hlsl", "PS", "VS");
}

void LoadState::OnActivated()
{
	DX11::ClearColour(m_clearCol.x, m_clearCol.y, m_clearCol.z, m_clearCol.w);
	// load game objs
	
	TheDX11TextureManager::Instance()->LoadTexture(L"../../Assets/loading_images_gameobjs.png");
	UpdateBackgroundTexture(L"../../Assets/loading_images_gameobjs.png");

	TheScreenManager::Instance()->FlipBuffers();
	//TheConfigManager::Instance()->Load("../../Config/config.info");
	TheConfigManager::Instance()->LoadInfoSettings("../../Config/maze.info");
	//LoadGameObjects(L"../../Game_Objects/test_gameobjects.txt");

	//TheGame::Instance()->SetGameState(TheTestState::Instance());
	TheGame::Instance()->SetGameState(TheTitleScreenState::Instance());
}

void LoadState::OnDeactivated()
{
}